package com.xcf.server;

import com.xcf.center.ServerMeta;
import com.xcf.center.server.IRegister;
import com.xcf.server.config.ProviderConfig;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SeverContext implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        ProviderConfig config = applicationContext.getBean(ProviderConfig.class);
        try {
            NettyStarter.startServer(applicationContext, config.getHost(), config.getPort());
            IRegister register = applicationContext.getBean(IRegister.class);
            register.init();
            ServerMeta serverMeta = new ServerMeta();
            serverMeta.setIp(config.getHost());
            serverMeta.setPort(config.getPort());
            register.register(serverMeta);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
